<?php
/**
 * Description of GraphDrawer
 *
 * @author dgorkovets
 */

include_once 'Graph.php';
include_once 'Plot.class.php';
//include_once 'BezierCurve.class.php';
include_once 'CubicSpline.class.php';

class GraphDrawer {
    private $image;
    private $graph;
    private $active_cycle;

    public function  __construct($xmldata)
    {
        $this->graph = new Graph($xmldata);
        $this->image = imagecreatetruecolor($this->graph->getWidth(),
                                            $this->graph->getHeight());
        //imageantialias($this->image, true);

        // sets background to white
        $white = imagecolorallocate($this->image, 225, 225, 225);
        imagefilltoborder($this->image, 0, 0, $white, $white);

        // define black and blue colours
        $black = imagecolorallocate($this->image, 0, 0, 0);
        $blue = imagecolorallocate($this->image, 0, 0, 255);
    }

    public function  __destruct() {
        imagedestroy($this->image);
    }

    public function getImage()
    {
        return $this->image;
    }

    public function setActiveCycle($ac)
    {
        $this->active_cycle = $ac;
    }

    public function process()
    {
        
        $tmpImage = imagecreatetruecolor($this->graph->getWidth()*2,
                                            $this->graph->getHeight()*2);

        // Drawing vertices
        foreach($this->graph->getVertices() as $v)
        {
            imagefilledellipse($tmpImage, $v->getX(), $v->getY(), 5, 5, $black);
        }

        //Drawing edges
        foreach($this->graph->getEdges() as $e)
        {
            // Draw Label
            imagestring($tmpImage,
                    5,
                    $e->label->x,
                    $e->label->y,
                    $e->label->text,
                    $black);
            
            $prevkp = $e->keypoints[0];
            foreach ($e->keypoints as $kp)
            {
                imageline($tmpImage, 2*$prevkp->getX(), 2*$prevkp->getY(), 2*$kp->getX(), 2*$kp->getY(), $black);
                $prevkp = $kp;
            }

            $coords = array();
            foreach ($e->keypoints as $kp)
            {
                //$coords[$kp->x] = $kp->y;
                
            }
            $coords[98.5466] = 303.895;
            $coords[97.3762] = 294.191;
            $coords[96.6848] = 279.842;
            $coords[100.372] = 268;
            $coords[102.21] = 262.099;
            $coords[105.714] = 256.337;
            $coords[109.282] = 251.525;

            $blue = imagecolorallocate($this->image, 0, 0, 255);
            foreach($coords as $x => $y)
            {
                //imagefilledellipse($this->getImage(), $x, $y, 5, 5, $blue);
            }


            $bk = new CubicSpline();
            $bk->setCoords($coords, 10);
            if (!$bk->getError())
            {
            $plot = new Plot($bk->process());
            //$plot->drawAALine($this->getImage(), $black);
            }

            //imagecopyresampled($this->image, $tmpImage, 0, 0, 0, 0, , $dst_h, $src_w, $src_h)
            //$tmpImage
            

        }
    }
}
?>
